#include "client.h"
int sockfd;
int ret_value;
void show(int fd)
{
	int opt;
	sockfd = fd;
	system("clear");
	puts("\t\t*************************");
	puts("\t		员工管理系统	   ");
	puts("\t\t1.	管理员登陆");
	puts("\t\t2.	员工  登陆");
	puts("\t\t3.	退      出");
	puts("\t\t*************************");
	if(ret_value == 12)
	{
		printf("账号或密码有误重新输入\n");
	}
	else if(ret_value == 13)
	{
		printf("输入有误请重新输入\n");
	}
	scanf("%d",&opt);
	getchar();
	switch(opt)
	{
		case 1:
				ret_value = admin_login();
				break;
		case 2:
				ret_value = employee_login();
				break;
		case 3:
				exit(0);
		default:
				ret_value = 13;
	}
}

int admin_login()
{
	char buf[1024] = "";
	char key[1024] = "";
	key[0] = '1';
	send(sockfd,&key,sizeof(key),0);//发送判断选项
	printf("请输入账号\n");
	fgets(buf,sizeof(buf),stdin);
	buf[strlen(buf)-1]='\0';
	send(sockfd,buf,sizeof(buf),0);
	printf("请输入密码\n");
	fgets(buf,sizeof(buf),stdin);
	buf[strlen(buf)-1]='\0';
	send(sockfd,buf,sizeof(buf),0);
	recv(sockfd,buf,sizeof(buf),0);//接收信号
	if(strncmp(buf,"10",2)==0)
	{	
		return 12;
	}
	else if(strncmp(buf,"11",2)==0){}
	system("clear");
	while(1){
	puts("***********************************************************");
	puts("\t\t		管理员系统	   ");
	puts("\t\t\t1.	员工信息增加");
	puts("\t\t\t2.	员工信息修改");
	puts("\t\t\t3.	员工信息删除");
	puts("\t\t\t4.	员工信息查找");
	puts("\t\t\t5.	查看员工查询纪录");
	puts("\t\t\t6.	返回上一级目录");
	puts("***********************************************************");
	fgets(buf,sizeof(buf),stdin);
	switch(buf[0])
	{
		case '1':
				key[0] = '1';
				send(sockfd,key,sizeof(key),0);
				memset(buf,0,sizeof(buf));
				admin_opt1(1);
				break;
		case '2':
				key[0] = '2';
				send(sockfd,key,sizeof(key),0);
				memset(buf,0,sizeof(buf));
				admin_opt1(2);
				break;
		case '3':
				key[0] = '3';
				send(sockfd,key,sizeof(key),0);
				memset(buf,0,sizeof(buf));
				admin_opt2(3);
				break;
		case '4':
				key[0] = '4';
				send(sockfd,key,sizeof(key),0);
				memset(buf,0,sizeof(buf));
				admin_opt2(4);
				break;
		case '5':
				key[0] = '5';
				send(sockfd,key,sizeof(key),0);
				memset(buf,0,sizeof(buf));
				admin_opt2(4);
				break;
		case '6':
				key[0] = '6';
				send(sockfd,key,sizeof(key),0);
				memset(buf,0,sizeof(buf));
				return 1;
		default:
				printf("输入有误\n");
	}
	
	}

}
int employee_login()
{
	char buf[1024]="";
	char key[1024]="";
	key[0]='2';
	send(sockfd,key,sizeof(key),0);
	printf("请输入账号\n");
	fgets(buf,sizeof(buf),stdin);
	buf[strlen(buf)-1]='\0';
	send(sockfd,buf,sizeof(buf),0);
	printf("请输入密码\n");
	fgets(buf,sizeof(buf),stdin);
	buf[strlen(buf)-1]='\0';
	send(sockfd,buf,sizeof(buf),0);
	recv(sockfd,buf,sizeof(buf),0);
 if(strncmp(buf,"12",2)==0)
	{
		printf("用户密码或账号错误\n");
		return 12;
	}
	else if(strncmp(buf,"13",2)==0)	{}
	while(1){
	puts("***********************************************************");
	puts("\t\t		员工查询系统	   ");
	puts("\t\t\t1.	员工信息查找");
	puts("\t\t\t2.	返回上一级目录");
	puts("***********************************************************");
	fgets(buf,sizeof(buf),stdin);
	printf("buf = %s\n",buf);
	switch(buf[0]){
		case '1':
				key[0] = '1';
				send(sockfd,key,sizeof(key),0);
				memset(buf,0,sizeof(buf));
				employee_opt(1);
				break;
		case '2':
				key[0] = '2';
				send(sockfd,key,sizeof(key),0);
				memset(buf,0,sizeof(buf));
				return 1;
		default:
				printf("输入有误\n");
				return 12;
							}
	memset(buf,0,sizeof(buf));
					}
}
int admin_opt1(int opt_num)
{
	emp_inf inf;
	char buf[1024];
	int ret;
	if(opt_num == 1)
	{
			printf("请输入想要添加的员工信息\n");
	}else if(opt_num == 2)
	{
			printf("请输入想要修改的员工信息\n");
	}
	printf("请输入员工姓名\n");
	fgets(inf.name,sizeof(inf.name),stdin);
	inf.name[strlen(inf.name)-1]='\0';
	printf("请输入员工年龄\n");
	fgets(inf.age,sizeof(inf.age),stdin);
	inf.age[strlen(inf.age)-1]='\0';
	printf("请输入员工电话\n");
	fgets(inf.tel,sizeof(inf.tel),stdin);
	inf.tel[strlen(inf.tel)-1]='\0';
	printf("请输入员工地址\n");
	fgets(inf.addr,sizeof(inf.addr),stdin);
	inf.addr[strlen(inf.addr)-1]='\0';
	printf("请输入员工职位\n");
	fgets(inf.job,sizeof(inf.job),stdin);
	inf.job[strlen(inf.job)-1]='\0';
	printf("请输入员工入职时间(xxxx.xx.xx)\n");
	fgets(inf.date,sizeof(inf.date),stdin);
	inf.date[strlen(inf.date)-1]='\0';
	printf("请输入员工薪资\n");
	fgets(inf.salary,sizeof(inf.salary),stdin);
	inf.salary[strlen(inf.salary)-1]='\0';
	printf("请输入员工评级\n");
	fgets(inf.grade,sizeof(inf.grade),stdin);
	inf.grade[strlen(inf.grade)-1]='\0';
	printf("请输入员工账号\n");
	fgets(inf.account,sizeof(inf.account),stdin);
	inf.account[strlen(inf.account)-1]='\0';
	printf("请输入员工密码\n");
	fgets(inf.pass,sizeof(inf.pass),stdin);
	inf.pass[strlen(inf.pass)-1]='\0';
	if(opt_num == 1)//添加信息
	{
		send(sockfd,&inf,sizeof(inf),0);
		recv(sockfd,buf,sizeof(buf),0);
		if(strncmp(buf,"14_1",4)==0)
		{
		printf("信息添加失败\n");
		return 14;
		}
		else if(strncmp(buf,"14_2",4)==0)
		{
		printf("信息添加成功\n");
		}
		recv(sockfd,buf,sizeof(buf),0);
		if(strncmp(buf,"15_1",4)==0)
		{
		printf("账号密码添加失败\n");
		return 15;
		}
		else if(strncmp(buf,"15_2",4)==0)
		{
		printf("账号密码添加成功\n");
		}
	}
	else if(opt_num == 2)
	{
		send(sockfd,&inf,sizeof(inf),0);
		recv(sockfd,buf,sizeof(buf),0);
		if(strncmp(buf,"16",2)==0)
		{
		printf("信息修改失败\n");
		return 16;
		}
		else if(strncmp(buf,"17",2)==0)
		{
		printf("信息修改成功\n");
		}
	}
}
int admin_opt2(int opt_num)
{
	emp_inf inf;
	char buf[1024];
	int num,i,j,ret;
	memset(buf,0,sizeof(buf));
	if(opt_num == 3)//添加信息
	{
		printf("请输入想要删除的员工姓名与年龄\n");
	}else if(opt_num == 4 | opt_num == 5) 
	{
		printf("请输入想要查找的员工姓名与年龄\n");
	}
		printf("请输入员工姓名\n");
		fgets(inf.name,sizeof(inf.name),stdin);
		inf.name[strlen(inf.name)-1]='\0';
		printf("请输入员工年龄\n");
		fgets(inf.age,sizeof(inf.age),stdin);
		inf.age[strlen(inf.age)-1]='\0';
		send(sockfd,&inf,sizeof(inf),0);
		recv(sockfd,buf,sizeof(buf),0);
		if(strncmp(buf,"18",2)==0)
		{
		printf("信息删除失败\n");
		return 18;
		}
		else if(strncmp(buf,"19",2)==0)
		{
		printf("信息删除成功\n");
		}
		else if(strncmp(buf,"20",2)==0)
		{
		printf("信息查找失败\n");
		return 20;
		}
		else if(strncmp(buf,"21",2)==0)
		{
		printf("信息查找成功\n");
		ret = recv(sockfd,&num,sizeof(num),0);//接收数据行数
		for(i=0;i<=num;i++)
		{
			for(j=0;j<8;j++)
			{
				recv(sockfd,buf,sizeof(buf),0);
				printf("%-10s",buf);
			}
			putchar(10);
		}
		}
		else if(strncmp(buf,"22",2)==0)
		{
		printf("历史纪录查找失败\n");
		return 22;
		}
		else if(strncmp(buf,"23",2)==0)
		{
		printf("历史纪录查找成功\n");
		ret = recv(sockfd,&num,sizeof(num),0);//接收数据行数
		for(i=0;i<=num;i++)
		{
			for(j=0;j<2;j++)
			{
				recv(sockfd,buf,sizeof(buf),0);
				printf("%-10s",buf);
			}
			putchar(10);
		}
		}
	
}
int employee_opt(int opt_num)
{
		char buf[1024];
		emp_inf inf;
		int i,j,num,ret;
		printf("请输入员工姓名\n");
		fgets(inf.name,sizeof(inf.name),stdin);
		inf.name[strlen(inf.name)-1]='\0';
		printf("请输入员工年龄\n");
		fgets(inf.age,sizeof(inf.age),stdin);
		inf.age[strlen(inf.age)-1]='\0';
		send(sockfd,&inf,sizeof(inf),0);//发送结构体
		recv(sockfd,buf,sizeof(buf),0);
		if(strncmp(buf,"26",2)==0)
		{
		printf("员工只能查询本人信息\n");
		return 26;
		}
		else if(strncmp(buf,"24",2)==0)
		{
		printf("信息查找失败\n");
		return 24;
		}
		else if(strncmp(buf,"25",2)==0)
		{
		printf("信息查找成功\n");
		recv(sockfd,&num,sizeof(num),0);//接收数据行数
		for(i=0;i<=num;i++)
			{
			for(j=0;j<8;j++)
			{
				ret = recv(sockfd,buf,sizeof(buf),0);
				printf("%-7s",buf);
			}
			putchar(10);
			}
		}
}